<template>
  <div class="alert">
    <Bbb></Bbb>
  </div>
</template>
<script>
  import Bbb from './B'
  let seed = 0

  function getUuid() {
    return 'alert_' + (++seed)
  }

  export default {
    name: 'componentA',
    inject: ['app'],
    created(){
      this.test = this.app.test
      this.$on('on-msg', this.showMsg)
    },
    components: {Bbb},
    provide:{
      name: 1,
      test: null
    },
    data() {
      return {
        notices: [] // 维护通知list
      }
    },
    methods: {
      showMsg(msg){
        alert(msg)
      },
      add(notice) {
        const name = getUuid()
        let _notice = Object.assign({ name: name }, notice)
        this.notices.push(_notice)

        const duration = notice.duration
        setTimeout(() => {
          this.remove(name)
        }, duration * 1000 || 2000)
      },
      remove(name) {
        const index = this.notices.findIndex(item => item.name === name)
        this.notices.splice(index, 1)
      }
    }
  }
</script>
<style scoped rel="stylesheet/stylus">
</style>
